<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Member_model extends CI_Model {  
    //查询是否注册过
    public function check_register($phone){
       $data = $this->db
            ->select('id, phone')
            ->where('phone', $phone)
            ->limit(1)->get('28qh_user');
        return $data->row(); 
    }
    //登陆 
    public function login($username, $pwd){
        $this->load->database();
        $result = $this->db
            ->select('*')
            ->where('username', $username)  
            ->where('password', $pwd)
            ->where('status', 1)
            ->limit(1)->get('wechat_admin');
        return $result->row();
    }
    
    //检查用户状态
    public function check_status($phone, $pwd){
        $data = $this->db
            ->select('uid, status')
            ->where('phone', $phone)
            ->where('upassword', $pwd)
            ->limit(1)->get('28qh_user');
        return $data->row();
    }
    
    //注册
    public function register($phone, $pwd,$payid,$name='默认'){
        $data = array(
            "phone" => $phone,
            'upassword' => $pwd,
            'payid' => $payid,
            'uname' => $name,
            'status' => '1',
            'createtime' => time()
        );
        $this->db->insert('28qh_user', $data);
        return $this->db->affected_rows();
    }
	
	//注册已重复手机号，修改其密码
    public function regUpdate_pwd($phone, $pwd){
        $data = array(
            'upassword' => $pwd
        );
        $this->db->where('phone', $phone)->update('28qh_user',$data);
        return $this->db->affected_rows(); 
    }
    
    //登录日志
    public function login_log($uid,$ip){
        $data = array(
			"uid" => $uid,
            "login_ip" => $ip
        );
        $this->db->insert('yjy_log', $data);
        return $this->db->affected_rows();
    }
    
    //验证数据是否存在
    public function isExist($uid, $phone){
        $this->db
            ->where('uid', $uid)  
            ->or_where('phone', $phone)
            ->limit(1)->get('yjy_user');
        return $this->db->affected_rows();
    }
    
    //首页喊单
    public function hdIndex(){
    	$ctime = date('Y-m-d');
        $data = $this->db->order_by("hdtime", "DESC")->where('hdtime >',$ctime.' 00:00:00')->where('hdtime <', $ctime.' 23:59:59')->where('type', 0)->select("*")->limit(1)->get("yjy_plan");
        return $data->result_array();
    }
    
	//首页喊单、提示列表
    public function hdList(){
    	$ctime = date('Y-m-d');
        $data = $this->db->where('hdtime >',$ctime.' 00:00:00')->where('hdtime <', $ctime.' 23:59:59')->select("*")->get("yjy_plan");
        return $data->result_array();
    }
    
 	//讲师列表
    public function techs(){
        $data = $this->db->where("valid",1)->where('uid !=',null)->order_by("level", "DESC")->select("uid,tname,level,photo,introduction,skill")->get("yjy_teacher");
        return $data->result_array();
    }
    
    //课表排班
 	public function teachclass(){
        $data= $this->db->query("SELECT t.id,t.ctime,
								t1.tname tname1,
								t2.tname tname2,
								t3.tname tname3,
								t4.tname tname4,
								t5.tname tname5 
								FROM yjy_class t 
								left join yjy_teacher t1 on t.week1=t1.id
								left join  yjy_teacher t2 on t.week2=t2.id
								left join  yjy_teacher t3 on t.week3=t3.id
								left join  yjy_teacher t4 on t.week4=t4.id
								left join  yjy_teacher t5 on t.week5=t5.id order by t.sort"); 
        return $data->result_array();
    }
    
	public function WebBase(){
        $dataq= $this->db->query("SELECT SubType,Content FROM sys_web where Type=1"); 
        $glaq=$dataq->result_array();
		foreach ($glaq as $web)
            { 
             switch($web["SubType"])
             {
             	case 1:$GLOBALS["yjy_config"]["title"]=$web["Content"];break;
             	case 2:$GLOBALS["yjy_config"]['keywords']=$web["Content"];break;
             	case 3:$GLOBALS["yjy_config"]['description']=$web["Content"];break;
             	case 4:$GLOBALS["yjy_config"]['tongji']=$web["Content"];break;
             	case 5:$GLOBALS["yjy_config"]['qq']=$web["Content"];break;
             	case 6:$GLOBALS["yjy_config"]['logo']=$web["Content"];break;
             	case 7:$GLOBALS["yjy_config"]['fxsinaweibo']=$web["Content"];break;
             	case 8:$GLOBALS["yjy_config"]['phone400']=$web["Content"];break;
             } 
         }
         $GLOBALS["yjy_config"]["skinbg"]=$this->skinbg();
	  }
	  
	  //皮肤切换
	public function skinbg(){
        $data= $this->db->query("SELECT  bg.bg,bg.ico_bg FROM  yjy_skin sk 
								inner join   yjy_skin_bg bg on sk.id=bg.type 
								where sk.valid=1 and bg.bg  !=''  order by bg.sort asc;"); 
        return $data->result_array();
	}
	  
	//老师详情
	public function teacherDetail($tuid){
	  	$data = $this->db->select('tname,level,skill,introduction,photo,valid')
            ->where('uid', $tuid)
            ->where('valid', 1)
            ->limit(1)->get('yjy_teacher');
        return $data->result_array();
	}
	
	//老师做单盈利率
	public function profitTotal($tuid){
		$data = $this->db->select_sum('mavs')
			->select_sum('pl')
    		->where('uid', $tuid)
    		->get('yjy_tradelist');
    	return $data->result_array();
	}
	
	//老师盈利单数
	public function profitAccount($tuid){
		$data = $this->db->select('id,uid,user')
    		->where('uid', $tuid)
    		->where('pl >',0)
    		->get('yjy_tradelist');
    	return $data->num_rows();
	}
	
	//老师亏损单数
	public function lossAccount($tuid){
		$data = $this->db->select('id,uid,user')
    		->where('uid', $tuid)
    		->where('pl <',0)
    		->get('yjy_tradelist');
    	return $data->num_rows();
	}
	
	//老师持仓记录
	public function teacherHold($tuid){
		$data = $this->db->select('cname,sprice,direction,sprofit,sloss,otime')
            ->where('uid', $tuid)
            ->where('status', 0)
            ->order_by('otime','desc')
            ->limit(10)->get('yjy_order');
        return $data->result_array();
	}
	
	//老师交易记录
	public function teacherTrade($tuid, $time){
		if($time == ""){
			$data = $this->db->select('tradetime,name,bsflag,quantity,pl')
	            ->where('uid', $tuid)
	            ->order_by('tradetime','desc')
	            ->get('yjy_tradelist');
		}else{
			$data = $this->db->select('tradetime,name,bsflag,quantity,pl')
	            ->where('uid', $tuid)
	            ->like('tradetime', $time, 'both')
	            ->order_by('tradetime','desc')
	            ->get('yjy_tradelist');
		}
        return $data->result_array();
	}
	
	//周六4点强制平仓
	public function comClose(){
		$data = array(
            'status' => 1
        );
		$this->db->where('status', 0)->update('yjy_order',$data);
        return $this->db->affected_rows();
	}
	
	//游客IP写入
	public function visitor($vip, $varea){
		$data = array(
			"ip" => $vip,
            "area" => $varea
        );
        $this->db->insert('yjy_visitor', $data);
        return $this->db->affected_rows();
	}
}
?>